//
//  CYVisitorTableViewController.swift
//  CYWeiBo
//
//  Created by Mac on 16/7/10.
//  Copyright © 2016年 Mac. All rights reserved.
//

import UIKit

class CYVisitorTableViewController: UITableViewController {

    //登录标示
    var isLogin: Bool = CYUserAccountViewModel.shareUserAccount.isLogin
    
    //访客视图 全局的 继承于CYVisitorView
    var visitorView: CYVisitorView?
    
    override func loadView() {
        if isLogin {//登录
            super.loadView()
        } else {
            //  未登录,需要提供访客视图
            visitorView = CYVisitorView()
            //  使用[weak self]解决闭包循环引用 传入一个闭包
            visitorView?.loginClosure = { [weak self] in
                self?.showOAuthLogin()
            }
            
            view = visitorView
            
            setupNavUI()
            
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    //  设置导航视图
    private func setupNavUI() {
    
        //   代码抽取到UIBarButtonItem分类中0
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: "registerAction")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: "loginAction")
    }
    
    // MARK:---点击事件
    @objc private func registerAction() {
        print("注册按钮点击了")
        
        showOAuthLogin()
    }
    
    @objc private func loginAction() {
        print("登录按钮点击了")
        showOAuthLogin()
    }
    
    //  OAuth 登录页面
    private func showOAuthLogin() {
    
        let oAuthVC = CYOAuthViewController()
        
        let nav = UINavigationController(rootViewController: oAuthVC)
        
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
}
